package com.dms.service.impl;

import com.alibaba.fastjson.JSON;
import com.dms.mapper.AdminManageMapper;
import com.dms.pojo.Admin;
import com.dms.pojo.Building;
import com.dms.pojo.LevelOneMenu;
import com.dms.pojo.User;
import com.dms.service.AdminManageService;
import com.dms.utils.MD5Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: UserServiceImpl
 * @package: com.dms.service.impl
 * @description
 * @author: sk
 */
@Service
public class AdminManageServiceImpl implements AdminManageService {

    @Autowired
    AdminManageMapper userMapper;

    /**
     *  用户登录
     * @param userName
     * @param password
     * @param userType
     * @return
     */
    @Override
    public User userLogin(@Param("userName") String userName, @Param("password") String password, @Param("userType") Integer userType) {
        User user = userMapper.getUserInfo(userName);
        //解密
        String str = MD5Util.convertMD5(MD5Util.convertMD5(user.getUserPassword()));
        if (!str.equals(password))
            return null;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userName", userName);
        map.put("password", password);
        map.put("userType", userType);
        return userMapper.userLogin(map);
    }

    /**
     * 查询用户记录总数
     * @param query
     * @return
     */
    @Override
    public int selectCount(@Param("query") String query){
        return userMapper.selectCount(query);
    }

    /**
     * 分页查询管理员信息
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Admin> findUserByLimit(String query, int pageNum, int pageSize){
        Map<String,Object> map = new HashMap<String, Object>();
        //计算分页大小
        int start = (pageNum - 1) * pageSize;
        map.put("query", query);
        map.put("start", start);
        map.put("size", pageSize);
        return userMapper.findUserByLimit(map);
    }

    /**
     * 用户名是否已存在验证
     * @param userName
     * @return
     */
    public Admin checkUserName(String userName){
        return userMapper.checkUserName(userName);
    }

    /**
     * 根据id修改用户 status 状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateUserStatus(@Param("id") int id, @Param("status") boolean status){
        return userMapper.updateUserStatus(id, status);
    }

    /**
     * 1、 添加新的管理员用户 在dms_admin表中
     * @param admin
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int addAdmin(Admin admin){
        //根据身份证辨别男女
        admin.setAdminGender(Integer.parseInt(admin.getAdminIdentityCardNumber().substring(16,17)) % 2 == 0 ? 0 : 1);
        String[] str = admin.getAdminIdentityCardNumber().split("&");
        admin.setAdminIdentityCardNumber(str[0]);
        String userPassword = str[1];
        //插入admin表
        int result = userMapper.addAdmin(admin);
        if (result == 1) {
            //将账号、密码、登录级别、adminId录入到user表中
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("userName", admin.getUserName());
            map.put("userPassword", userPassword);
            map.put("userType", admin.getUserType());
            map.put("adminId", getAdminId(admin.getUserName()));
            result += addUser(map);
        }
        return result;
    }

    /**
     * 获取admin的Id
     * @param userName
     * @return
     */
    public int getAdminId(String userName){
        return userMapper.getAdminId(userName);
    }

    /**
     *  3、 在dms_user表中，插入登录数据
     * @param map
     * @return
     */
    @Override
    public int addUser(Map<String, Object> map) {
        return userMapper.addUser(map);
    }

    /**
     * 根据id查询用户信息
     * @param adminId
     * @return
     */
    @Override
    public Admin getAdminInfo(Integer adminId){
        return userMapper.getAdminInfo(adminId);
    }

    /**
     * 修改管理员用户信息
     * @param admin
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int editAdminInfo(Admin admin){
        if (admin.getBuildingId() != null){
            admin.setBuildingId( admin.getBuildingId() == -1  ? null : admin.getBuildingId());
        }
        int num = userMapper.editAdminInfo(admin);
        //将账号、密码、登录级别录入到user表中
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("userName", admin.getUserName());
        map.put("userPassword", admin.getUser().getUserPassword());
        num += editUserInfo(map);  //调用修改用户表
        return num;
    }
    /**
     * 修改用户信息
     * */
    @Override
    public int editUserInfo(Map<String,Object> map){
        return userMapper.editUserInfo(map);
    }

    /**
     * 删除管理员用户信息  dms_user表 和 dms_admin表
     * */
    @Override
    public int deleteAdminInfo(String userName){
        return userMapper.deleteAdminInfo(userName);
    }

    /**
     *  获取楼宇列表
     *  */
    @Override
    public List<Building> getBuildingList(){
        return userMapper.getBuildingList();
    }


    /* ********************************************************/

    /**
     * 获取角色权限列表
     * @return
     */
    @Override
    public List<LevelOneMenu> getAuthorityList(int userType) {
        return userMapper.getAuthorityList(userType);
    }

    /**
     * 修改角色权限
     * @param arr
     * @param noSelectArr
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int updateAuthority(String arr,String noSelectArr) {
        List noSelectArrList = (List) JSON.parse(noSelectArr);
        List arrList = (List) JSON.parse(arr);
        int num = 0;
        num += userMapper.updateAuthorityShow(arrList);
        if (!noSelectArrList.isEmpty()){
            num += userMapper.updateAuthorityNoShow(noSelectArrList);
        }
        System.out.println(num);
        return num;
    }
}
